<?php

class set_image
{
	public $pos_list = null ;
	
	private $bk_mp_img = 0 ;		// 是否又毛坯图。
	private $dst_image = null ;
	private $dst_width = 0 ;
	private $dst_height = 0 ;
	private $type = 0 ;
	
	// 自动生成并且保存某个套装组合成一张图片.
	function createAndSave_Set($setid)
	{
		$this->type = 1 ;
		$rnt = false ;
		$this->pos_list = sql_fetch_rows("select * from taozhuang_pos where `tzid` = $setid and `order` = 0 order by `pos` asc");
		if ($this->pos_list != null && is_array($this->pos_list) == true && count($this->pos_list) > 0)
		{
			$this->bk_mp_img = intval(sql_fetch_one_cell("select `back_img` from `taozhuang` where `sid` = '$setid'")) ;
			$rnt = $this->createNewImage(960 , 720) ;
		}
		
		if ($rnt == true){
			$tzone = sql_fetch_one("select * from taozhuang where sid = '$setid'") ;
			if (is_null($tzone) == false && is_array($tzone)){
				$img_small_id = $tzone['img_small_id'] ;
				$img_thumb_id = $tzone['img_thumb_id'] ;
				$img_url_id = $tzone['img_url_id'] ;
				
				$val_t = intval(sql_fetch_one_cell("select config_value from prf_config where `id` = 15")) ;
				if ($val_t <= 20)		$val_t = 20 ;	// 套装封面中图
				$val_s = intval(sql_fetch_one_cell("select config_value from prf_config where `id` = 16")) ;
				if ($val_s <= 10)		$val_s = 10 ;	// 套装封面小图
				
				$newid = $this->imgChangeSave($img_small_id , $tzone['name'] , $val_s / 100 , "images/taozhuang/icon/small_") ;
				sql_query("update taozhuang set `img_small_id` = '$newid' where `sid` = '$setid'") ;
				$newid = $this->imgChangeSave($img_thumb_id , $tzone['name'] , $val_t / 100 , "images/taozhuang/thumb/thumb_") ;
				sql_query("update taozhuang set `img_thumb_id` = '$newid' where `sid` = '$setid'") ;
				$newid = $this->imgChangeSave($img_url_id   , $tzone['name'] , 1  , "images/taozhuang/big/big_") ;
				sql_query("update taozhuang set `img_url_id` = '$newid' where `sid` = '$setid'") ;
			}
		}
		
		return $rnt ;
	}
	function createAndSave_Diy($diyid)
	{
		$this->type = 2 ;
		$rnt = false ;
		$this->pos_list = sql_fetch_rows("select * from diy_pos where `diy_id` = $diyid order by `pos` asc");
		if ($this->pos_list != null && is_array($this->pos_list) == true && count($this->pos_list) > 0)
		{
			$this->bk_mp_img = intval(sql_fetch_one_cell("select `back_img` from `diy` where `id` = '$diyid'")) ;
			$rnt = $this->createNewImage(960 , 720) ;
		}
		
		if ($rnt == true){
			$tzone = sql_fetch_one("select * from diy where id = '$diyid'") ;
			if (is_null($tzone) == false && is_array($tzone)){
				$img_small_id = $tzone['img_small_id'] ;
				$img_thumb_id = $tzone['img_thumb_id'] ;
				$img_url_id = $tzone['img_url_id'] ;
				
				$val_t = intval(sql_fetch_one_cell("select config_value from prf_config where `id` = 15")) ;
				if ($val_t <= 20)		$val_t = 20 ;	// 套装封面中图
				$val_s = intval(sql_fetch_one_cell("select config_value from prf_config where `id` = 16")) ;
				if ($val_s <= 10)		$val_s = 10 ;	// 套装封面小图
				
				$newid = $this->imgChangeSave($img_small_id , $tzone['name'] , $val_s / 100 , "images/taozhuang/icon/small_") ;
				sql_query("update diy set `img_small_id` = '$newid' where `id` = '$diyid'") ;
				$newid = $this->imgChangeSave($img_thumb_id , $tzone['name'] , $val_t / 100 , "images/taozhuang/thumb/thumb_") ;
				sql_query("update diy set `img_thumb_id` = '$newid' where `id` = '$diyid'") ;
				$newid = $this->imgChangeSave($img_url_id   , $tzone['name'] , 1  , "images/taozhuang/big/big_") ;
				sql_query("update diy set `img_url_id` = '$newid' where `id` = '$diyid'") ;
			}
		}
		
		return $rnt ;
	}
	function createAndSave_MyDiy($diyid)
	{
		$this->type = 3 ;
		$rnt = false ;
		$this->pos_list = sql_fetch_rows("select ut.* , t.`pos`,t.`father`,t.`x`,t.`y`,t.`anchor_x`,t.`anchor_y`,t.`rotate`,t.`zoom`,t.`mirror` from user_diy_pos ut inner join diy_pos t on ut.`pos_id` = t.`id` where ut.`user_diy_id` = '$diyid' order by t.`pos` asc");
		if ($this->pos_list != null && is_array($this->pos_list) == true && count($this->pos_list) > 0)
		{
			$this->bk_mp_img = intval(sql_fetch_one_cell("select t.`back_img` from user_diy ut inner join diy t on ut.`diy_id` = t.`id` where ut.`id` = '$diyid'")) ;
			$rnt = $this->createNewImage(960 , 720) ;
		}
		
		if ($rnt == true){
			$tzone = sql_fetch_one("select * from user_diy where id = '$diyid'") ;
			if (is_null($tzone) == false && is_array($tzone)){
				$img_small_id = $tzone['img_small_id'] ;
				$img_thumb_id = $tzone['img_thumb_id'] ;
				$img_url_id = $tzone['img_url_id'] ;
				
				$val_t = intval(sql_fetch_one_cell("select config_value from prf_config where `id` = 15")) ;
				if ($val_t <= 20)		$val_t = 20 ;	// 套装封面中图
				$val_s = intval(sql_fetch_one_cell("select config_value from prf_config where `id` = 16")) ;
				if ($val_s <= 10)		$val_s = 10 ;	// 套装封面小图
				
				$newid = $this->imgChangeSave($img_small_id , $tzone['name'] , $val_s / 100 , "images/newtaozhuang/mydiysuolue/icon/small_" , 416) ;
				sql_query("update user_diy set `img_small_id` = '$newid' where `id` = '$diyid'") ;
				$newid = $this->imgChangeSave($img_thumb_id , $tzone['name'] , $val_t / 100 , "images/newtaozhuang/mydiysuolue/thumb/thumb_" , 416) ;
				sql_query("update user_diy set `img_thumb_id` = '$newid' where `id` = '$diyid'") ;
				$newid = $this->imgChangeSave($img_url_id   , $tzone['name'] , 1  , "images/newtaozhuang/mydiysuolue/big/big_" , 416) ;
				sql_query("update user_diy set `img_url_id` = '$newid' where `id` = '$diyid'") ;
			}
		}
		
		return $rnt ;
	}
	
	private function imgChangeSave($imgid , $imgname , $zoom , $save_path , $img_type_group = 415)
	{
		$dw = intval($this->dst_width * $zoom) ;
		$dh = intval($this->dst_height * $zoom) ;
		$img_one = sql_fetch_one("select * from sys_image where `id` = $imgid") ;
		if (is_null($img_one) == false && is_array($img_one) == true){
			$save_path = $img_one['url'] ;
			sql_query("update sys_image set `type` = $img_type_group , `width` = '$dw' , `height` = '$dh' where `id` = $imgid") ;
		}else{
			$imgid = sql_insert("insert into sys_image(`type` , `name` , `width` , `height` , `url`) values ('415' , '$imgname' , '$dw' , '$dh' , '')") ;
			$save_path .= $imgid."_auto.jpg" ;
			sql_query("update sys_image set `type` = $img_type_group , `url` = '$save_path' , `update_time` = unix_timestamp() where `id` = $imgid") ;
		}
		$save = $GLOBALS['upload_Path'].$save_path ;
		make_dir(getPath_dir($save)) ;
		if ($zoom == 1){		
			imagejpeg($this->dst_image , $save , 100) ;
		}else{
			$d_img = $this->imageZoom($this->dst_image , $this->dst_width , $this->dst_height , $zoom) ;
			imagejpeg($d_img , $save , 100) ;
		}
		return $imgid ;
	}
	
	private function getImage($path)
	{
		if (strlen($path) == 0)		return null ;
		$rnt = getimagesize($path) ;
		$src_image = null ;
		if ($rnt['mime'] == "image/png"){
			$src_image = imagecreatefrompng($path) ;
			imagesavealpha($src_image,true);			// 不要丢了$thumb图像的透明色;
		}else if ($rnt['mime'] == "image/jpeg" || $rnt['mime'] == "image/jpg"){
			$src_image = imagecreatefromjpeg($path) ;
		}else{
			return null ;
		}
		return array($src_image , $rnt[0] , $rnt[1] , $rnt) ;
		
	}
	
	private function imageScale($image , $scaleX , $scaleY)
	{
		$thumbw = imagesx($image); // 原图宽
		$thumbh = imagesy($image); // 原图高

		if (function_exists("imagecreatetruecolor")){
    		$dim = imagecreatetruecolor($thumbw, $thumbh); // 创建目标图gd2
		}else{
    		$dim = imagecreate($thumbh, $thumbw); // 创建目标图gd1
		}
		imagealphablending($dim,false); 	// 不合并颜色,直接用其他图像颜色替换,包括透明色;
		imagesavealpha($dim,true);			// 不要丢了$thumb图像的透明色;
		
		if ($scaleX == true && $scaleY == false){
	    	for($x=0; $x<$thumbw; $x++) {
	        	for($y=0; $y<$thumbh; $y++) {
	            	imagecopyresampled($dim, $image, $thumbw-$x-1, $y, $x, $y, 1, 1, 1, 1); //水平翻转
	        	}
	    	}
		}
		if ($scaleX == false && $scaleY == true){
	    	for($x=0; $x<$thumbw; $x++) {
	        	for($y=0; $y<$thumbh; $y++) {
	            	imagecopyresampled($dim, $image, $x, $thumbh-$y-1, $x, $y, 1, 1, 1, 1); //垂直翻转
	        	}
	    	}
		}
		if ($scaleX == true && $scaleY == true){
	    	for($x=0; $x<$thumbw; $x++) {
	        	for($y=0; $y<$thumbh; $y++) {
	            	imagecopyresampled($dim, $image, $thumbw-$x-1, $thumbh-$y-1, $x, $y, 1, 1, 1, 1); //垂直并且水平翻转
	        	}
	    	}
		}
		return $dim ;
	}
	private function imageZoom($image , $w , $h , $zoom)
	{
		$dst_image = imagecreatetruecolor($w * $zoom , $h * $zoom) ;

		$dst_x = 0 ;				// 设定需要载入的图片在新图中的x坐标
		$dst_y = 0 ;				// 设定需要载入的图片在新图中的y坐标
		$src_x = 0 ;				// 设定载入图片要载入的区域x坐标
		$src_y = 0 ;				// 设定载入图片要载入的区域y坐标
		$dst_w = $w * $zoom ; 		// 设定载入的原图的宽度（在此设置缩放）
		$dst_h = $h * $zoom ; 		// 设定载入的原图的高度（在此设置缩放）
		$src_w = $w ;				// 原图要载入的宽度
		$src_h = $h ;				// 原图要载入的高度
		
		imagecopyresampled($dst_image, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) ;
		return $dst_image ;
	}
	private function copyImageToImage($srcimage , $desimage , $w , $h , $pos_item)
	{
		$des = $desimage ;
		
		if ($pos_item != null && is_array($pos_item) == true){
			if ($pos_item['rotate'] != 0)		$des = imagerotate($des , intval($pos_item['rotate']) , 0) ;	// 旋转
			if ($pos_item['mirror'] == 1)		$des = $this->imageScale($des , true , false) ;
			if ($pos_item['mirror'] == 2)		$des = $this->imageScale($des , false , true) ;
			if ($pos_item['mirror'] == 3)		$des = $this->imageScale($des , true , true) ;
		}
		
		$dst_x = 0 ;				// 设定需要载入的图片在新图中的x坐标
		$dst_y = 0 ;				// 设定需要载入的图片在新图中的y坐标
		$src_x = 0 ;				// 设定载入图片要载入的区域x坐标
		$src_y = 0 ;				// 设定载入图片要载入的区域y坐标
		$dst_w = $w  ; 				// 设定载入的原图的宽度（在此设置缩放）
		$dst_h = $h  ; 				// 设定载入的原图的高度（在此设置缩放）
		$src_w = $w ;				// 原图要载入的宽度
		$src_h = $h ;				// 原图要载入的高度
		
		if ($pos_item != null && is_array($pos_item) == true){
			$dst_w = $w * floatval($pos_item['zoom']) ;
			$dst_h = $h * floatval($pos_item['zoom']) ;
			$dst_x = intval($pos_item['x']) - $w * floatval($pos_item['anchor_x']) * floatval($pos_item['zoom']) ;
			$dst_y = intval($pos_item['y']) - $h * floatval($pos_item['anchor_y']) * floatval($pos_item['zoom']) ;
		}

		imagecopyresampled($srcimage, $des, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) ;
		
		return array($dst_x , $dst_y) ;
	}
	
	private function createNewImage($width , $height)
	{
		$this->dst_image = imagecreatetruecolor($width , $height) ;
		$this->dst_width = $width ;
		$this->dst_height = $height ; 
		
		if ($this->bk_mp_img > 0){
			$val = $this->get_image_for_id($this->bk_mp_img) ;
			if ($val[0] > 0){
				
				$posimg = $this->getImage($val[4]) ;
				if ($posimg != null)
				{
					$this->copyImageToImage($this->dst_image , $posimg[0] , $posimg[1] , $posimg[2] , null) ;
				}
			}
		}
		
		$parent = array() ;
		$two = array() ;
		foreach ($this->pos_list as $pos){
			if ($pos['father'] == 0){
				$val = $this->get_image_values($pos['id']) ;
				$posimg = $this->getImage($val[6]) ;
				if ($posimg != null)
				{
					$pos['new_xy'] = $this->copyImageToImage($this->dst_image , $posimg[0] , $posimg[1] , $posimg[2] , $pos) ;					
				}else{
					$pos['new_xy'] = array($pos['x'],$pos['y']) ;
				}
				$pos['new_wh'] = array($val[4],$val[5]) ;
				$parent[$pos['pos']] = $pos ;
			}else{
				$two[] = $pos ;
			}
		}
		while (count($two) > 0){
			$three = array() ;
			foreach ($two as $pos){
				$fdt = $parent[$pos['father']] ;
				if ($fdt != null && array_key_exists("new_xy", $fdt)){
					$pos['x'] += $fdt['new_xy'][0] + $fdt['new_wh'][0] * floatval($fdt['zoom']) * floatval($fdt['anchor_x']) ;
					$pos['y'] += $fdt['new_xy'][1] + $fdt['new_wh'][1] * floatval($fdt['zoom']) * floatval($fdt['anchor_y']) ;
					$val = $this->get_image_values($pos['id']) ;
					$posimg = $this->getImage($val[6]) ;
					if ($posimg != null)
					{
						$pos['new_xy'] = $this->copyImageToImage($this->dst_image , $posimg[0] , $posimg[1] , $posimg[2] , $pos) ;					
					}else{
						$pos['new_xy'] = array($pos['x'],$pos['y']) ;
					}
					$pos['new_wh'] = array($val[4],$val[5]) ;
					$parent[$pos['pos']] = $pos ;
				}else{
					$three[] = $pos ;
				}
			}
			$two = $three ;
		}
		
		return true ;
	}
	
	private function get_image_values($pos_id)
	{
		$val = array() ;
		$test = new test_bk() ;		
		if ($this->type == 1){
			$val = $test->get_taozhuang_pos_for_ts_image($pos_id) ;
		}elseif ($this->type == 2){		
			$val = $test->get_diy_pos_for_ts_image($pos_id) ;
		}else{
			$val = $test->get_diy_pos_for_ts_image($pos_id , 2) ;
		}
		return $val ;
	}
	private function get_image_for_id($imgid)
	{
		$val = array() ;
		$test = new test_bk() ;		
		$val = $test->get_image_for_id($imgid) ;
		return $val ;
	}
}

// 通过图片Id列表组合成一张图片,存到第一张图片所在位置.
function img_set_imglists_toOne($img_str_list , $savename , $old_imgid)
{
	$ones = sql_fetch_one("select max(`width`) as max_width , max(`height`) as max_height , max(`type`) as max_type from sys_image where `id` in ($img_str_list)") ;
	$img_array = explode("," , $img_str_list) ;
	$max_width = $ones['max_width'] ;
	$max_height = $ones['max_height'] ;
	$max_type = $ones['max_type'] ;
	
	$tyone = sql_fetch_one("select * from prf_type where `id` = '$max_type'") ;
	$addr  = $tyone['addr'] ;
	$fne = explode(".",$savename) ;
	if (count($fne) >= 2){
		$fne = "." . $fne[count($fne) - 1] ;
	}else{
		$fne = ".mc.jpg" ;
	}
	
	$path = "images/" . $addr . "/" . $savename ;
	$save_path_over = $GLOBALS['upload_Path'] . $path ;
		
		
	$dst_image = imagecreatetruecolor($max_width , $max_height) ;
	
	foreach($img_array as $imgid){
		$img_one = sql_fetch_one("select * from sys_image where `id` = $imgid") ;
		if (is_null($img_one) == false && is_array($img_one) == true){
			$save_path = $img_one['url'] ;
		}else{
			continue ;
		}
		$fileaddr = $GLOBALS['upload_Path'].$save_path ;
		
		$rnt = getimagesize($fileaddr) ;
		$src_image = null ;
		if ($rnt['mime'] == "image/png"){
			$src_image = imagecreatefrompng($fileaddr) ;
			imagesavealpha($src_image,true);			// 不要丢了$thumb图像的透明色;
			if ($src_image != null){
				imagecopyresampled($dst_image, $src_image, 0, 0, 0, 0, $max_width, $max_height, $max_width, $max_height) ;
			}
		}		
	}
	
	imagejpeg($dst_image , $save_path_over , 100) ;
	
	if ($old_imgid == 0){
		$newid = sql_insert("insert into sys_image(`type` , `name` , `width` , `height`, `url` , `update_time`) values ('$max_type' , '$savename' , '$max_width' , '$max_height' , '$path' , unix_timestamp())") ;
	}else{
		$newid = $old_imgid ;
		if (intval(sql_fetch_one_cell("select `id` from `sys_image` where `id` = '$newid'")) == $newid){
			sql_query("update `sys_image` set `url` = '$path', `update_time` = unix_timestamp() , `type` = '$max_type' , `name` = '$savename'  , `width` = '$max_width' , `height` = '$max_height' where `id` = $newid") ;
		}else{
			$newid = sql_insert("insert into sys_image(`type` , `name` , `width` , `height`, `url` , `update_time`) values ('$max_type' , '$savename' , '$max_width' , '$max_height' , '$path' , unix_timestamp())") ;
		}
	}
	
	$url = $GLOBALS['URL_IMG_HEAD'].$path ;
	
	return array($newid , $save_path_over , $url) ;
}


?>